package com.monicash.monicash.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseActivity;
import com.monicash.monicash.information.CapitalInforActivity;
import com.monicash.monicash.login.modle.LoginModle;
import com.monicash.monicash.util.IntentUtils;
import com.monicash.monicash.util.T;

import java.util.HashMap;
import java.util.Map;

public class RegisterActivity extends BaseActivity {

    TextView back;
    TextView right_text;

    EditText et_password;
    ImageView iv_look_up;
    EditText et_password_confirm;
    ImageView iv_look_up_confirm;
    TextView tv_button;
    TextView tv_runtime_error;

    EditText et_invitation_code;
    CheckBox cb_1;
    TextView tv_1;

    CheckBox cb_2;
    TextView tv_2;

    private String password = "";
    private String password_confirm = "";

    private String invita_code = "";
    private boolean look_up_confirm = false;
    private boolean look_up = false;
    String  account = "";
    String code = "";
    LoginModle loginModle = null;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Intent intent = getIntent();
        if (intent != null) {
            String ss = intent.getStringExtra("account");
            String code = intent.getStringExtra("code");
            if (ss != null) {
                account = ss;
            }
            if (code != null) {
                this.code = code;
            }
        }
        loginModle = LoginModle.Companion.getInstant();
        initView();
        initData();

    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        back = findViewById(R.id.back);
        right_text = findViewById(R.id.right_text);
        et_password = findViewById(R.id.et_password);
        iv_look_up = findViewById(R.id.iv_look_up);
        et_password_confirm = findViewById(R.id.et_password_confirm);
        iv_look_up_confirm = findViewById(R.id.iv_look_up_confirm);
        tv_button = findViewById(R.id.tv_button);
        tv_runtime_error = findViewById(R.id.tv_runtime_error);

        et_invitation_code = findViewById(R.id.et_invitation_code);
        cb_1 = findViewById(R.id.cb_1);
        tv_1 = findViewById(R.id.tv_1);

        cb_2 = findViewById(R.id.cb_2);
        tv_2 = findViewById(R.id.tv_2);

        tv_1.setText(getSpannableString1());

        tv_1.setText(getSpannableString1());

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invita_code = et_invitation_code.getText().toString();
                if(TextUtils.isEmpty(password)){
                    T.showShort(getActivity(),"密码未输入");
                    return;
                }
                if(TextUtils.isEmpty(password_confirm)){
                    T.showShort(getActivity(),"密码未确认");
                    return;
                }
                if(!cb_1.isChecked()){
                    T.showShort(getActivity(),"协议未同意");
                    return;
                }
                if(!cb_2.isChecked()){
                    T.showShort(getActivity(),"协议未同意");
                    return;
                }

                Map<String, String> params = new HashMap<String, String>();
                params.put("country", "51");
                params.put("mobile", account);
                params.put("smsCode", code);
                params.put("password", password);
                params.put("rePassword", password_confirm);
                params.put("inviteCode", invita_code);
                params.put("channel", "");
                params.put("registerChannel", "");
                params.put("latitude", "");
                params.put("longitude", "");
                params.put("fillInInfoGroup", "");
                String ss = loginModle.accountZiLiPageRegister(params);
                Intent intent = new Intent(RegisterActivity.this, CapitalInforActivity.class);
                startActivity(intent);
                finish();
            }
        });
        iv_look_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                look_up = !look_up;
                if(look_up){
                    et_password.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else {
                    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }

            }
        });
        iv_look_up_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                look_up_confirm = !look_up_confirm;
                if(look_up_confirm){
                    et_password_confirm.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else {
                    et_password_confirm.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                password = s.toString();
            }
        });

        et_password_confirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                password_confirm = s.toString();
                if(!TextUtils.equals(password_confirm,password)){
                    tv_runtime_error.setVisibility(View.VISIBLE);
                }else {
                    tv_runtime_error.setVisibility(View.GONE);
                }

            }
        });

    }

    @Override
    public void initData() {

    }


    private SpannableString getSpannableString1() {
        String keyword1 = "Términos";
        String contentText = "Estoy de acuerdo con los Términos y la Política de Privacidad";
        int indexKeyword1Start = contentText.indexOf(keyword1);
        int indexKeyword1End = indexKeyword1Start + keyword1.length();
        SpannableString spannableString = new SpannableString(contentText);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                String androidButtonUrl = "https://www.baidu.com";
                if (!TextUtils.isEmpty(androidButtonUrl)) {
                    IntentUtils.startBrowser((Context) RegisterActivity.this, androidButtonUrl);
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(RegisterActivity.this.getColor( R.color.blue_00daf7));// 字体颜色
                ds.setUnderlineText(false); // 是否有下划线
            }
        }, indexKeyword1Start, indexKeyword1End, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        String keyword2 = "olítica de Privacidad";
        int indexKeyword2Start = contentText.indexOf(keyword2);
        int indexKeyword2End = indexKeyword2Start + keyword2.length();

        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                String androidButtonUrl = "https://www.baidu.com";
                if (!TextUtils.isEmpty(androidButtonUrl)) {
                    IntentUtils.startBrowser((Context) RegisterActivity.this, androidButtonUrl);
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(RegisterActivity.this.getColor( R.color.blue_00daf7));// 字体颜色
                ds.setUnderlineText(false); // 是否有下划线
            }
        }, indexKeyword2Start, indexKeyword2End, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        return spannableString;
    }

}
